package com.laot.searchManage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laot.searchManage.mapper.DataSourceSignMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.laot.seacher_master.model.common.ErrorCode;
import org.laot.seacher_master.model.common.PageRequest;
import org.laot.seacher_master.model.exception.BusinessException;
import org.laot.searchManage.dto.DataSourceSignSaveDto;
import org.laot.searchManage.entry.DataSourceSign;
import org.laot.searchManage.vo.DataSourceSignPageVo;
import org.laot.searchManage.vo.DataSourceSignVo;
import org.laot.searchManage.service.DataSourceSignService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author laoT
* @description 针对表【data_source_sign(数据源登记表)】的数据库操作Service实现
* @createDate 2024-01-22 19:48:36
*/
@Service
public class DataSourceSignServiceImpl extends ServiceImpl<DataSourceSignMapper, DataSourceSign>
    implements DataSourceSignService {
    @Resource
    private DataSourceSignMapper dataSourceSignMapper;

    @Override
    public List<DataSourceSignVo> listAll() {
        return dataSourceSignMapper.listAll();
    }

    @Override
    public Page<DataSourceSignPageVo> pageInfo(PageRequest pageRequest) {
        if(pageRequest == null) throw new BusinessException(ErrorCode.PARAMS_ERROR);
        long currentIndex = 1L;
        long pageSize = 20L;
        if(pageRequest.getCurrent() != 0){
            currentIndex = pageRequest.getCurrent();
        }
        if(pageRequest.getPageSize() != 0){
            pageSize = pageRequest.getPageSize();
        }
        Page<DataSourceSign> page = new Page<>(currentIndex,pageSize);
        Page<DataSourceSignPageVo> returnPage = new Page<>(currentIndex,pageSize);
        //查询条件
        LambdaQueryWrapper<DataSourceSign> queryWrapper = new LambdaQueryWrapper<>();
        this.page(page,queryWrapper);
        List<DataSourceSign> records = page.getRecords();
        if(CollectionUtils.isNotEmpty(records)){
            List<DataSourceSignPageVo> list = new ArrayList<>();
            for (DataSourceSign dataSourceSign : records) {
                DataSourceSignPageVo dataSourceSignPageVo = new DataSourceSignPageVo();
                BeanUtils.copyProperties(dataSourceSign,dataSourceSignPageVo);
                list.add(dataSourceSignPageVo);
            }
            returnPage.setRecords(list);
        }
        returnPage.setTotal(page.getTotal());
        return returnPage;
    }

    @Override
    public void saveInfo(DataSourceSignSaveDto dto, String userName) {
        //校验传入参数是否合法
        verifyParam(dto);
        DataSourceSign dataSourceSign = new DataSourceSign();
        BeanUtils.copyProperties(dto,dataSourceSign);
        dataSourceSign.setCreateUser(userName);
        //进行保存
        this.baseMapper.insert(dataSourceSign);
    }



    @Override
    public void updateInfo(DataSourceSignSaveDto dto, String userName) {
        //校验传入参数是否合法
        verifyParam(dto);
        //判断id是否存在
        if(dto.getId() == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"id不能为空！");
        }
        //查询对应数据源登记
        DataSourceSign dataSourceSign = this.getById(dto.getId());
        dataSourceSign.setCode(dto.getCode());
        dataSourceSign.setName(dto.getName());
        dataSourceSign.setAuthor(dto.getAuthor());
        dataSourceSign.setBeanName(dto.getBeanName());
        //进行保存
        this.baseMapper.updateById(dataSourceSign);
    }
    private void verifyParam(DataSourceSignSaveDto dto) {
        if(dto == null) throw new BusinessException(ErrorCode.PARAMS_ERROR);
        if(StringUtils.isBlank(dto.getName())) throw new BusinessException(ErrorCode.PARAMS_ERROR,"名字不能为空！");
        if(StringUtils.isBlank(dto.getCode())) throw new BusinessException(ErrorCode.PARAMS_ERROR,"编码不能为空！");
        if(StringUtils.isBlank(dto.getBeanName())) throw new BusinessException(ErrorCode.PARAMS_ERROR,"bean名称不能为空！");
        if (StringUtils.isBlank(dto.getAuthor())) throw new BusinessException(ErrorCode.PARAMS_ERROR,"作者不能为空！");
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        if(id == null) throw new BusinessException(ErrorCode.PARAMS_ERROR);
        if(status == null) throw new BusinessException(ErrorCode.PARAMS_ERROR);
        if(status!=1 && status!=0) throw new BusinessException(ErrorCode.PARAMS_ERROR);
        //获取相应数据源登记信息
        DataSourceSign dataSourceSign = this.getById(id);
        if(dataSourceSign == null) throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"该数据源信息不存在！");
        dataSourceSign.setStatus(status);
        this.baseMapper.updateById(dataSourceSign);
    }
}




